<?php

class Contato extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
	}
	
	public function index()
	{
		$data['titulo'] = 'Formulário de Contato';
		$this->load->library('javascript');
		$this->load->library('input');
		
		$cap = $this->criaCaptcha();
		$data['captcha'] = $cap['image'];
		
		$data2 = array(
			'captcha_time' => $cap['time'],
			'ip_address' => $this->input->ip_address(),
			'word' => $cap['word']
			);
		$query = $this->db->insert_string('captcha', $data2);
		$this->db->query($query);
		
		
		
		//print_r($this->javascript);
		$this->load->view("includes/header",$data);
		$this->load->view("includes/menu");
		$this->load->view("contato/contato_view",$data);
		$this->load->view("includes/footer");
	}
	
	public function envia()
	{
		$erro = false;
		$data['titulo'] = 'Formulário de Contato';
		
		$this->load->library('form_validation');
		$this->load->library('email');
		
		$this->form_validation->set_rules('txtEmail','Email','trim|required|valid_email|xxs_clean');
		
		
		//verifica o captcha
		$expiration = time()-7200; // Two hour limit
		$this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);
		
		// Then see if a captcha exists:
		$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address =
		? AND captcha_time > ?";
		$binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
		$query = $this->db->query($sql, $binds);
		$row = $query->row();
		if ($row->count == 0)
		{
			$this->session->set_flashdata('msg','O tetxo na imagem informada está errado');
			$erro = true;
		}
		
		
		
		if(!$erro)
		{
			if($this->form_validation->run() == false)
			{
				//$this->session->set_flashdata('msg','Inválido!');
				//redirect('contatos/contato');
				
				$cap = $this->criaCaptcha();
				$data['captcha'] = $cap['image'];
				
				$this->load->view('includes/header');
				$this->load->view('includes/menu');
				$this->load->view('contato/contato_view',$data);
				$this->load->view('includes/footer');
			}
			else 
			{
				$this->email->from('faturamento@centralserver.com.br');
				$this->email->to('diegues@netnigro.com.br');
				$this->email->cc('diegues@netnigro.com.br');
				$this->email->bcc('diegues@netnigro.com.br');
				$this->email->subject('Email Test');
				$this->email->message('Test Message');
				$this->email->send();
				
				//echo $this->email->print_debugger();
				
				$this->session->set_flashdata('msg','Email enviado com sucesso!');
				redirect('contatos/contato');
			}
		}
		else 
		{
			redirect('contatos/contato');
		}
		
	}
	
	
	/**
	 * Esta funcao tem por objetivo criar um objeto do tipo captcha
	 * @author Diegues Roveda
	 * @return array contendo as informacoes do captcha
	 * 
	 */
	
	protected function criaCaptcha()
	{
		$this->load->helper('captcha');
		$this->load->helper('string');
		
		$word = random_string('alpha',8);
		
			$vals = array(
			'word' => $word,
			'img_path' => './captcha/',
			'img_url' => 'http://localhost/fat/captcha/',
			'font_path' => './system/fonts/textb.ttf',
			'img_width' => '150',
			'img_height' => 30,
			'expiration' => 7200
		);
		
		$cap = create_captcha($vals);
		
		return $cap;
	}
	
}


?>